package com.common.weibolib;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.common.weibolib.NdColumns.FlowerMsgColumns;
import com.common.weibolib.NdColumns.PraiseUserColumns;
import com.common.weibolib.NdColumns.ReplyColumns;
import com.common.weibolib.NdColumns.TimeStampColumns;
import com.common.weibolib.NdColumns.TopicInfoColumns;
import com.common.weibolib.NdColumns.UserColumns;
import com.product.android.commonInterface.weibo.FlowerMessageInfo;

public class NdWeiboDatabase {
	private SQLiteDatabase mDb;
	private static NdWeiboDatabase _instance;

	private static long preUid = 0;

	private NdWeiboDatabase(Context context) {
		if (preUid == 0)
			preUid = GlobalSetting.getUid();
		NdWeiboDataHelper helper = new NdWeiboDataHelper(context, preUid);
		mDb = helper.getWritableDatabase();
	}

	public synchronized static NdWeiboDatabase getInstance(Context context) {
		long curuid = GlobalSetting.getUid();
		if (preUid != curuid) {
			preUid = curuid;
			// 先关闭之前的
			if (_instance != null) {
				_instance.closeDB();
			}

			_instance = new NdWeiboDatabase(context);
		} else if (_instance == null || 
				_instance.mDb == null || !_instance.mDb.isOpen()) {
			_instance = new NdWeiboDatabase(context);
		}
		return _instance;
	}

	public void closeDB() {
		if (mDb != null && mDb.isOpen()) {
			mDb.close();
			mDb = null;
		}

	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getFriendsTopicInfoList
	 * @brief	获取关注好友的微博列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @return
	 * @return TopicInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-13下午8:09:29
	 */
	public TopicInfoList getFriendsTopicInfoList(){
		TopicInfoList tweetList = new TopicInfoList();
		String orderby = TopicInfoColumns.POST_TIME +" DESC LIMIT 20";
		Cursor cursor = mDb.query(NdWeiboDataHelper.FRIEND_TOPIC_TABLE, null,
				null, null, null, null, orderby);
		if(cursor != null){
			try {
				while (cursor.moveToNext()) {
					tweetList.add(getTopicInfoFromCursor(cursor));
				}
			} finally{
				cursor.close();
			}
		}
		return tweetList;
	}
			
	/**
	 * 
	 * @n<b>函数名称</b>     :getPublicTopicInfoList
	 * @brief	获取公共微博列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @return
	 * @return TopicInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:16:46
	 */
	public TopicInfoList getPublicTopicInfoList() {
		TopicInfoList tweetList = new TopicInfoList();
		Cursor cursor = mDb.query(NdWeiboDataHelper.PUBLIC_TOPIC_TABLE, null,
				null, null, null, null, TopicInfoColumns.POST_TIME
						+ " DESC LIMIT 20");
		if(cursor != null){
			try {
				while (cursor.moveToNext()) {
					tweetList.add(getTopicInfoFromCursor(cursor));
				}
			} finally{
				cursor.close();
			}
		}

		return tweetList;
	}
	
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getLastOrOldestTopicInfo
	 * @brief	获取最新或者最旧的微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:19:38
	 */
	private TopicInfo getLastOrOldestTopicInfo(String type,String table) {
		String orderBy = TopicInfoColumns.POST_TIME + " " + type +" LIMIT 1";
		Cursor cursor = mDb.query(table, null, null,null, null, null, orderBy);
		if(cursor == null) return null;
		try {
			if (cursor.moveToFirst()) {
				return getTopicInfoFromCursor(cursor);
			}
		} finally{
			cursor.close();
		}
		return null;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getLastTopicId
	 * @brief	获取指定表中已读过的最新微博id
	 * @see
	 * @since    Ver 1.1
	 * @param  @return
	 * @return long
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-13下午5:06:12
	 */
	public long getLastTopicId(String table){
		TopicInfo info = getLastOrOldestTopicInfo("DESC", table);
		return info == null ? 0 : info.tid;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getPublicTopicInfoById
	 * @brief 从公共微博表中查找指定微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param id
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17下午2:33:57
	 */
	public TopicInfo getPublicTopicInfoById(long id) {
		
		return getTopicInfoById(id, NdWeiboDataHelper.PUBLIC_TOPIC_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getTopicInfoById
	 * @brief	根据id获取指定微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param id
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:20:56
	 */
	private TopicInfo getTopicInfoById(long id,String table) {
		String selection = TopicInfoColumns.ID + "=" + id;
		Cursor cursor = mDb.query(table, null,selection, null, null, null, null);
		if(cursor == null) return null;
		try {
			if (cursor.moveToFirst()) {
				return getTopicInfoFromCursor(cursor);
			}
		} finally{
			cursor.close();
		}
		return null;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :deleteTopicInfo
	 * @brief	删除指定微博，同时删除其他表可能存在用户微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param id
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:21:45
	 */
	public void deleteTopicInfo(long id) {
		String whereClause = TopicInfoColumns.ID + "=" + id;
		mDb.delete(NdWeiboDataHelper.PUBLIC_TOPIC_TABLE, whereClause, null);
		mDb.delete(NdWeiboDataHelper.USER_TOPIC_TABLE, whereClause, null);
		mDb.delete(NdWeiboDataHelper.FRIEND_TOPIC_TABLE, whereClause, null);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setPublicTopicInfoList
	 * @brief	设置公共微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param list
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-13上午11:30:38
	 */
	public void setPublicTopicInfoList(TopicInfoList list) {
		
		setTopicInfoList(null, 0, list, NdWeiboDataHelper.PUBLIC_TOPIC_TABLE);
	}
	
	public void setFriendTopicInfoList(TopicInfoList list) {
		
		setTopicInfoList(null, 0, list, NdWeiboDataHelper.FRIEND_TOPIC_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearTableByTableName
	 * @brief	清空表数据
	 * @see
	 * @since    Ver 1.1
	 * @param  @param table
	 * @param  @param uid
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-20下午4:32:21
	 */
	private void clearTableByTableName(String table,long uid){
		
		if(table.equals(NdWeiboDataHelper.PUBLIC_TOPIC_TABLE)){
			clearPublicTopicInfos();
		}
		else if(table.equals(NdWeiboDataHelper.USER_TOPIC_TABLE)){
			clearUserTopicInfoListById(uid);
		}
		else if(table.equals(NdWeiboDataHelper.AT_TOPIC_TABLE)){
			clearAtTopicInfos();
		}
		else if(table.equals(NdWeiboDataHelper.FRIEND_TOPIC_TABLE)){
			clearFriendTopicInfos();
		}
		else if(table.equals(NdWeiboDataHelper.RE_TOPIC_TABLE)){
			clearReTopicInfos();
		}
		else if(table.equals(NdWeiboDataHelper.AT_REPLY_TABLE)){
			clearAtReplyInfos();
		}
		else if(table.equals(NdWeiboDataHelper.REPLY_TABLE)){
			clearReplyInfos(uid);
		}
		else if(table.equals(NdWeiboDataHelper.REPLY_ME_TABLE)){
			clearReplyMe();
		}
		else if(table.equals(NdWeiboDataHelper.PRAISE_USER_TABLE)){
			clearPraisors();
		}
		else if(table.equals(NdWeiboDataHelper.MY_PRAISED_TOPIC_TABLE)){
			clearPraises();
		}
		else if(table.equals(NdWeiboDataHelper.TAG_TOPIC_TABLE)){
			clearTagTopicInfos();
		}
		
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setTopicInfoList
	 * @brief	保存微博列表的通用入口，指定保存到哪个表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param uid
	 * @param  @param list
	 * @param  @param table
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:42:33
	 */
	private void setTopicInfoList(String keyword,long uid,TopicInfoList list,String table) {
		mDb.beginTransaction();
		try {
			clearTableByTableName(table, uid);
			int size = ( list == null ? 0 : list.size() );
			if(size != 0){
				for (TopicInfo tweet : list) {
					setTopicInfo(keyword,uid, tweet, table);
				}
			}
			mDb.setTransactionSuccessful();
		} finally {
			mDb.endTransaction();
		}
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :deleteReplysByTid
	 * @brief	删除指定微博的所有评论
	 * @see
	 * @since    Ver 1.1
	 * @param  @param tid
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:25:11
	 */
	public void deleteReplysByTid(long tid) {
		String whereClause = ReplyColumns.TO_TID + "=" + tid;
		mDb.delete(NdWeiboDataHelper.REPLY_TABLE, whereClause, null);
		mDb.delete(NdWeiboDataHelper.REPLY_ME_TABLE, whereClause, null);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :deleteReplyByRid
	 * @brief	删除指定评论
	 * @see
	 * @since    Ver 1.1
	 * @param  @param rid
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:26:35
	 */
	public void deleteReplyByRid(long rid) {
		String whereClause = ReplyColumns.ID + "=" + rid;
		mDb.delete(NdWeiboDataHelper.REPLY_TABLE, whereClause, null);
		mDb.delete(NdWeiboDataHelper.REPLY_ME_TABLE, whereClause, null);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setPublicTopicInfo
	 * @brief	保存到公共微博库
	 * @see
	 * @since    Ver 1.1
	 * @param  @param topicInfo
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:46:30
	 */
	public void setPublicTopicInfo(TopicInfo topicInfo) {
		
		setTopicInfo(null,0,topicInfo,NdWeiboDataHelper.PUBLIC_TOPIC_TABLE);
	}
	
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setTopicInfo
	 * @brief	保存微博信息的通用入口，保存微博到指定数据库表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param keyword		话题微博专用字段，其他传null
	 * @param  @param uid			用户微博专用字段，其他传0
	 * @param  @param topicInfo	
	 * @param  @param table		存储数据的表名
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午9:55:05
	 */
	private void setTopicInfo(String keyword,long uid,TopicInfo topicInfo,String table) {
		
		if(topicInfo == null){
			return;
		}
		
		ContentValues values = new ContentValues();
		
		setValuesByTopicInfo(values, topicInfo);
		// 这个是用户微博表专用
		if(uid != 0 && values != null){
			values.put(TopicInfoColumns.UID, uid);
		}
		// 这个是话题表专用
		if(keyword != null && values != null){
			values.put(TopicInfoColumns.KEY_WORD, keyword);
		}
			
		mDb.replace(table, null, values);
		
		setUser(topicInfo.user);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setValuesByTopicInfo
	 * @brief	根据TopicInfo的值设置ContentValues
	 * @see
	 * @since    Ver 1.1
	 * @param  @param values
	 * @param  @param topicInfo
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:08:02
	 */
	public static void setValuesByTopicInfo(ContentValues values,TopicInfo topicInfo){
		if(values == null){
			values = new ContentValues();
		}
		values.clear();
		values.put(TopicInfoColumns.ID, topicInfo.tid);
		values.put(TopicInfoColumns.SINA_ID, topicInfo.weibo_tid);
		values.put(TopicInfoColumns.CONTENT, topicInfo.content);
		values.put(TopicInfoColumns.ARTICLE, topicInfo.article);
		values.put(TopicInfoColumns.CATEGORY, topicInfo.category);
		values.put(TopicInfoColumns.TYPE, topicInfo.type);
		values.put(TopicInfoColumns.FORWARDS, topicInfo.forwards);
		values.put(TopicInfoColumns.FROM_STRING, topicInfo.from_string);
		values.put(TopicInfoColumns.POST_TIME, topicInfo.post_time);
		values.put(TopicInfoColumns.GLANCES, topicInfo.glances);
		values.put(TopicInfoColumns.PRAISED, topicInfo.praised);
		values.put(TopicInfoColumns.PRAISES, topicInfo.praises);
		values.put(TopicInfoColumns.REPLYS, topicInfo.replys);
		values.put(TopicInfoColumns.ROOT_TID, topicInfo.root_tid);
		if(topicInfo.request_id != 0){
			values.put(TopicInfoColumns.REQUEST_ID, topicInfo.request_id);
		}
		
		UserInfo user = topicInfo.user;
		try {
			values.put(TopicInfoColumns.USER, new UserInfoParser().toJSONObject(user).toString());
			if(topicInfo.attach != null){
				values.put(TopicInfoColumns.ATTACH, new AttachListParser().toJSONArray(topicInfo.attach).toString());
			}
			if(topicInfo.image != null){
				values.put(TopicInfoColumns.IMAGE, new ImageListparser().toJSONArray(topicInfo.image).toString());
			}
			if(topicInfo.root_topic != null){
				values.put(TopicInfoColumns.ROOT_TOPIC, new RootTopicParser().toJSONObject(topicInfo.root_topic).toString());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearPublicTopicInfos
	 * @brief	清除公共微博表数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17下午2:45:02
	 */
	private void clearPublicTopicInfos() {
		mDb.delete(NdWeiboDataHelper.PUBLIC_TOPIC_TABLE, null, null);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearFriendTopicInfos
	 * @brief	清除关注微博表数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-13下午2:15:12
	 */
	private void clearFriendTopicInfos() {
		mDb.delete(NdWeiboDataHelper.FRIEND_TOPIC_TABLE, null, null);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setUserTopicInfoList
	 * @brief	保存用户个人主页微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param uid
	 * @param  @param list
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17下午2:52:56
	 */
	public void setUserTopicInfoList(long uid,TopicInfoList list){
		
		setTopicInfoList(null,uid, list, NdWeiboDataHelper.USER_TOPIC_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :addUserTopicInfo
	 * @brief	添加一条个人主页微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param uid
	 * @param  @param topicInfo
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:47:16
	 */
	public void setUserTopicInfo(long uid, TopicInfo topicInfo) {
		
		setTopicInfo(null,uid,topicInfo,NdWeiboDataHelper.USER_TOPIC_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearUserTopicInfoListById
	 * @brief	清除某个人的主页微博数据
	 * @see
	 * @since    Ver 1.1
	 * @param  @param uid
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:02:43
	 */
	private void clearUserTopicInfoListById(long uid) {
		String whereClause = TopicInfoColumns.UID + "=" + uid;
		mDb.delete(NdWeiboDataHelper.USER_TOPIC_TABLE, whereClause, null);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getOldestUserTopicInfo
	 * @brief	根据用户id获取数据库缓存的个人主页的最旧微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param uid
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:04:55
	 */
	public TopicInfo getOldestUserTopicInfo(long uid) {
		String selection = TopicInfoColumns.UID + "=" + uid;
		String orderBy = TopicInfoColumns.POST_TIME + " ASC limit 1";
		Cursor cursor = mDb.query(NdWeiboDataHelper.USER_TOPIC_TABLE, null,selection, null, null, null, orderBy);
		if(cursor == null) return null;
		try {
			if (cursor.moveToNext()) {
				return getTopicInfoFromCursor(cursor);
			}
		} finally{
			cursor.close();
		}
		return null;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getUserInfoByUid
	 * @brief	根据id获取指定用户信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param id
	 * @param  @return
	 * @return UserInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:07:04
	 */
	public UserInfo getUserInfoByUid(long id) {
		String selection = UserColumns.ID + "=" + id;
		Cursor cursor = mDb.query(NdWeiboDataHelper.USER_TABLE, null,
				selection, null, null, null, null);
		if(cursor == null) return null;
		try {
			if (cursor.moveToNext()) {
				return getUserFromCursor(cursor);
			}
		} finally{
			cursor.close();
		}
		
		return null;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setUser
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @param user
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17下午2:03:59
	 */
	public void setUser(UserInfo user) {
		if(user == null){
			return;
		}
		// set user
		ContentValues values = new ContentValues();
		values.put(UserColumns.ID, user.uid);
		values.put(UserColumns.NAME, user.nickname);
		values.put(UserColumns.GENDER, user.gender);

		// 用户可能已经在数据表里有了，所以用replace
		mDb.replace(NdWeiboDataHelper.USER_TABLE, null, values);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getOldestReplyInfo
	 * @brief	获取某条微博的最后一条评论
	 * @see
	 * @since    Ver 1.1
	 * @param  @param tweetId
	 * @param  @return
	 * @return ReplyInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:13:29
	 */
	public ReplyInfo getOldestReplyInfo(long tweetId) {
		
		String selection = ReplyColumns.TO_TID + "=" + tweetId;
		String orderBy = ReplyColumns.POST_TIME + " ASC LIMIT 1";
		Cursor cursor = mDb.query(NdWeiboDataHelper.REPLY_TABLE, null,
				selection, null, null, null, orderBy);
		if(cursor == null) return null;
		try {
			if (cursor.moveToFirst()) {
				return getReplyInfoFromCursor(cursor);
			}
		}finally{
			cursor.close();
		}
		return null;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getOldestReTopicInfo
	 * @brief	获取某条微博的最旧转发微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param tweetId
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:19:02
	 */
	public TopicInfo getOldestReTopicInfo(long tweetId) {
	
		String selection = TopicInfoColumns.ROOT_TID + "=" + tweetId;
		String orderBy = TopicInfoColumns.POST_TIME + " ASC LIMIT 1";
		Cursor cursor = mDb.query(NdWeiboDataHelper.RE_TOPIC_TABLE, null,
				selection, null, null, null, orderBy);
		if(cursor == null) return null;
		try {
			if (cursor.moveToFirst()) {
				return getTopicInfoFromCursor(cursor);
			}
		} finally{
			cursor.close();
		}
		return null;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getReplyInfoList
	 * @brief	获取某条微博的评论列表（指定时间内）
	 * @see
	 * @since    Ver 1.1
	 * @param  @param tweetId
	 * @param  @param beginTime
	 * @param  @param endTime
	 * @param  @return
	 * @return ReplyInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:15:21
	 */
	public ReplyInfoList getReplyInfoList(long tweetId) {
		
		String selection = ReplyColumns.TO_TID + "=" + tweetId;
		Cursor cursor = mDb.query(NdWeiboDataHelper.REPLY_TABLE, null,
				selection, null, null, null, ReplyColumns.POST_TIME
						+ " DESC LIMIT 20");
		if(cursor == null) return null;
		ReplyInfoList list = new ReplyInfoList();
		try {
			while (cursor.moveToNext()) {
				list.add(getReplyInfoFromCursor(cursor));
			}
		}finally{
			cursor.close();
		}
		return list;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getReTopicInfoList
	 * @brief	获取某条微博的转发列表（指定时间范围）
	 * @see
	 * @since    Ver 1.1
	 * @param  @param tweetId
	 * @param  @return
	 * @return TopicInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:21:21
	 */
	public TopicInfoList getReTopicInfoList(long tweetId) {
		
		String selection = TopicInfoColumns.ROOT_TID + "=" + tweetId;
		Cursor cursor = mDb.query(NdWeiboDataHelper.RE_TOPIC_TABLE, null,
				selection, null, null, null, TopicInfoColumns.POST_TIME
						+ " DESC LIMIT 20");
		if(cursor == null) return null;
		TopicInfoList list = new TopicInfoList();
		try {
			while (cursor.moveToNext()) {
				list.add(getTopicInfoFromCursor(cursor));
			}
		}finally{
			cursor.close();
		}
		return list;
	}

	
	/**
	 * 
	 * @n<b>函数名称</b>     :getPraisorList
	 * @brief	获取比传入的赞时间早的赞用户
	 * @see
	 * @since    Ver 1.1
	 * @param  @param tweetId
	 * @param  @return
	 * @return PraisorList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午4:22:45
	 */
	public PraisorList getPraisorList(long tweetId) {
		
		String selection = PraiseUserColumns.TWEET_ID + "=" + tweetId;
		Cursor cursor = mDb.query(NdWeiboDataHelper.PRAISE_USER_TABLE,
				null, selection, null, null, null, PraiseUserColumns.PRAISE_TIME
						+ " DESC LIMIT 20");
		if(cursor == null) return null;
		PraisorList list = new PraisorList();
		try {
			while (cursor.moveToNext()) {
				list.add(getPraisorFromCursor(cursor));
			}
		}finally{
			cursor.close();
		}
		return list;
	}

	/**
	 * 
	 * @param tid 
	 * @n<b>函数名称</b>     :setReplyInfoList
	 * @brief	保存评论列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param list
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:22:30
	 */
	public void setReplyInfoList(ReplyInfoList list, long tid) {

		setReplyInfoList(list, tid,NdWeiboDataHelper.REPLY_TABLE);
		
	}
	/**
	 * 
	 * @n<b>函数名称</b>     :setReplyInfo
	 * @brief	保存评论数据
	 * @see
	 * @since    Ver 1.1
	 * @param  @param comment
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:23:52
	 */
	public void setReplyInfo(ReplyInfo comment) {
		
		setReplyInfo(comment, NdWeiboDataHelper.REPLY_TABLE);
		
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :clearReplyInfo
	 * @brief	清除评论表数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午9:47:36
	 */
	private void clearReplyInfos(long tid) {
		
		mDb.delete(NdWeiboDataHelper.REPLY_TABLE, ReplyColumns.TO_TID + "=" + tid, null);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setReTopicInfoList
	 * @brief	保存转发列表信息到数据库
	 * @see
	 * @since    Ver 1.1
	 * @param  @param list
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午9:48:53
	 */
	public void setReTopicInfoList(TopicInfoList list) {

		setTopicInfoList(null, 0, list, NdWeiboDataHelper.RE_TOPIC_TABLE);
		
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setReTopicInfo
	 * @brief	设置转发数据
	 * @see
	 * @since    Ver 1.1
	 * @param  @param retweet
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午6:26:12
	 */
	public void setReTopicInfo(TopicInfo retweet) {
		
		setTopicInfo(null,0,retweet,NdWeiboDataHelper.RE_TOPIC_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearReTopicInfos
	 * @brief	清除转发表里面所有数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午9:58:31
	 */
	private void clearReTopicInfos() {
		mDb.delete(NdWeiboDataHelper.RE_TOPIC_TABLE, null, null);
	}
		
	/**
	 * 
	 * @n<b>函数名称</b>     :setPraisorList
	 * @brief	设置赞用户列表信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param userList
	 * @param  @param tid  对应的微博id
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-7下午5:36:43
	 */
	public void setPraisorList(PraisorList userList,long tid) {
		mDb.beginTransaction();
		try {
			mDb.delete(NdWeiboDataHelper.PRAISE_USER_TABLE, PraiseUserColumns.TWEET_ID + "=" + tid, null);
			int size = ( userList == null ? 0 : userList.size() );
			if(size != 0){
				for (Praisor praisor : userList) {
					setPraisor(praisor);
				}
			}
			mDb.setTransactionSuccessful();
		} finally {
			mDb.endTransaction();
		}
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setPraisor
	 * @brief	设置赞用户信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param praisor
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午4:27:41
	 */
	public void setPraisor(Praisor praisor) {
		if(praisor == null){
			return;
		}
		ContentValues values = new ContentValues();
		values.put(PraiseUserColumns.ID, praisor.id);
		values.put(PraiseUserColumns.TWEET_ID, praisor.tid);
		values.put(PraiseUserColumns.PRAISE_TIME, praisor.praise_time);
		try {
			values.put(PraiseUserColumns.USER, new UserInfoParser().toJSONObject(praisor.user).toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mDb.replace(NdWeiboDataHelper.PRAISE_USER_TABLE, null, values);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :clearPraisor
	 * @brief	清除赞用户表数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午4:28:16
	 */
	private void clearPraisors() {
		mDb.delete(NdWeiboDataHelper.PRAISE_USER_TABLE, null, null);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setAtTopicInfoList
	 * @brief	保存@我的微博到数据库
	 * @see
	 * @since    Ver 1.1
	 * @param  @param atList
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:27:27
	 */
	public void setAtTopicInfoList(TopicInfoList atList) {
		
		setTopicInfoList(null, 0, atList, NdWeiboDataHelper.AT_TOPIC_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setAtReplyInfoList
	 * @brief	保存@我的评论到数据库
	 * @see
	 * @since    Ver 1.1
	 * @param  @param replyList
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-7下午2:10:13
	 */
	public void setAtReplyInfoList(ReplyInfoList replyList) {
		
		setReplyInfoList(replyList, 0,NdWeiboDataHelper.AT_REPLY_TABLE);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getAtTopicInfoList
	 * @brief	获取@我的微博列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @return
	 * @return TopicInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:29:35
	 */
	public TopicInfoList getAtTopicInfoList() {
		String orderBy = TopicInfoColumns.POST_TIME + " DESC LIMIT 20 ";
		Cursor cursor = mDb.query(NdWeiboDataHelper.AT_TOPIC_TABLE, null,null, null, null, null, orderBy);
		if(cursor == null) return null;
		TopicInfoList atList = new TopicInfoList();
		try {
			while (cursor.moveToNext()) {
				atList.add(getTopicInfoFromCursor(cursor));
			}
		} finally {
			cursor.close();
		}

		return atList;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getAtReplyInfoList
	 * @brief	获取@我的评论列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param maxId
	 * @param  @return
	 * @return ReplyInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-7下午2:02:05
	 */
	public ReplyInfoList getAtReplyInfoList() {
		String orderBy = ReplyColumns.POST_TIME + " DESC LIMIT 20 ";
		Cursor cursor = mDb.query(NdWeiboDataHelper.AT_REPLY_TABLE, null,null, null, null, null, orderBy);
		if(cursor == null) return null;
		ReplyInfoList atList = new ReplyInfoList();
		try {
			while (cursor.moveToNext()) {
				atList.add(getReplyInfoFromCursor(cursor));
			}
		} finally {
			cursor.close();
		}

		return atList;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getLastOrOldestAtReplyInfo
	 * @brief	 获取数据库中最新或者最旧的一个@我的评论
	 * @see
	 * @since    Ver 1.1
	 * @param  @param order
	 * @param  @return
	 * @return ReplyInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:33:22
	 */
	public ReplyInfo getLastOrOldestAtReplyInfo(String order) {
		String orderBy = ReplyColumns.POST_TIME + order + " limit 1";
		Cursor cursor = mDb.query(NdWeiboDataHelper.AT_REPLY_TABLE, null, null,null, null, null, orderBy);
		if (cursor != null) {
			try {
				if (cursor.moveToNext()) {
					return getReplyInfoFromCursor(cursor);
				}
			}finally {
				cursor.close();
			}
		}
		return null;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getLastOrOldestAtTopicInfo
	 * @brief	获取数据库中最新或者最旧的一个@我的微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param order
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-7下午2:03:16
	 */
	public TopicInfo getLastOrOldestAtTopicInfo(String order) {
		String orderBy = TopicInfoColumns.POST_TIME + order + " limit 1";
		Cursor cursor = mDb.query(NdWeiboDataHelper.AT_TOPIC_TABLE, null, null,null, null, null, orderBy);
		if (cursor != null) {
			try {
				if (cursor.moveToNext()) {
					return getTopicInfoFromCursor(cursor);
				}
			}finally {
				cursor.close();
			}
		}
		return null;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :clearAtTopicInfos
	 * @brief	清除@我的微博数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:31:01
	 */
	private void clearAtTopicInfos() {
		mDb.delete(NdWeiboDataHelper.AT_TOPIC_TABLE, null, null);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearAtReplyInfos
	 * @brief	清除@我的评论数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-7下午2:08:01
	 */
	private void clearAtReplyInfos() {
		mDb.delete(NdWeiboDataHelper.AT_REPLY_TABLE, null, null);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setReplyMeList
	 * @brief	设置消息中心 我的微博评论
	 * @see
	 * @since    Ver 1.1
	 * @param  @param replyInfoList
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午3:59:34
	 */
	public void setReplyMeList(ReplyInfoList replyInfoList) {
		
		setReplyInfoList(replyInfoList,0, NdWeiboDataHelper.REPLY_ME_TABLE);
	}
	
	/**
	 * 
	 * @param tid 
	 * @n<b>函数名称</b>     :setReplyInfoList
	 * @brief	保存评论列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param replyInfoList
	 * @param  @param table
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17下午3:10:02
	 */
	private void setReplyInfoList(ReplyInfoList replyInfoList,long tid, String table) {
		mDb.beginTransaction();
		try {
			clearTableByTableName(table, tid);
			if (replyInfoList != null && replyInfoList.size() != 0) {
				for (ReplyInfo info : replyInfoList) {
					setReplyInfo(info,table);
				}
			}
			mDb.setTransactionSuccessful();
		} finally {
			mDb.endTransaction();
		}
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getReplyMeList
	 * @brief	获取消息中心 我的微博评论
	 * @see
	 * @since    Ver 1.1
	 * @param  @param maxId
	 * @param  @return
	 * @return ReplyInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午3:52:51
	 */
	public ReplyInfoList getReplyMeList(){
		String orderBy = ReplyColumns.POST_TIME + " DESC LIMIT 20";
		Cursor cursor = mDb.query(NdWeiboDataHelper.REPLY_ME_TABLE, null,null, null, null, null, orderBy);
		if(cursor == null) return null;
		ReplyInfoList replyInfoList = new ReplyInfoList();
		if (cursor != null) {
			try {
				while (cursor.moveToNext()) {
					replyInfoList.add(getReplyInfoFromCursor(cursor));
				}
			} finally {
				cursor.close();
			}
		}
		return replyInfoList;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setReplyInfo
	 * @brief	保存评论数据
	 * @see
	 * @since    Ver 1.1
	 * @param  @param info
	 * @param  @param table	插入的表
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:31:48
	 */
	private void setReplyInfo(ReplyInfo info,String table){
		if(info == null){
			return;
		}
		ContentValues values = new ContentValues();
		setValuesByReplyInfo(info, values);
		mDb.replace(table, null, values);
		setUser(info.user);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setValuesByReplyInfo
	 * @brief	根据ReplyInfo的值设置ContentValues
	 * @see
	 * @since    Ver 1.1
	 * @param  @param info
	 * @param  @param values
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:17:58
	 */
	public static void setValuesByReplyInfo(ReplyInfo info,ContentValues values){
		if(values == null){
			throw new NullPointerException("ContentValues Null");
		}
		values.clear();
		values.put(ReplyColumns.ID, info.rid);
		values.put(ReplyColumns.CONTENT, info.content);
		values.put(ReplyColumns.POST_TIME, info.post_time);
		values.put(ReplyColumns.FROM_STRING, info.from_string);
		values.put(ReplyColumns.TO_RID, info.to_rid);
		values.put(ReplyColumns.TO_TID, info.to_tid);
		values.put(ReplyColumns.UID, info.uid);
		if(info.request_id != 0){
			values.put(ReplyColumns.REQUEST_ID, info.request_id);
		}
		
		try {
			if(info.to_reply != null){
				values.put(ReplyColumns.TO_REPLY, new ToReplyParser().toJSONObject(info.to_reply).toString());
			}
			if(info.toTopic != null){
				values.put(ReplyColumns.ROOT_TOPIC, new RootTopicParser().toJSONObject(info.toTopic).toString());
			}
			if(info.user != null){
				values.put(ReplyColumns.USER, new UserInfoParser().toJSONObject(info.user).toString());
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getLastOrOldestReplyMe
	 * @brief	获取数据库中最新或者最旧的一个评论我的数据
	 * @see
	 * @since    Ver 1.1
	 * @param  @param type	DESC 最新， ASC 最旧
	 * @param  @return
	 * @return ReplyInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午4:04:06
	 */
	public ReplyInfo getLastOrOldestReplyMe(String type){
		String orderBy = ReplyColumns.POST_TIME + " " + type + " limit 1";
		Cursor cursor = mDb.query(NdWeiboDataHelper.REPLY_ME_TABLE, null,null, null, null, null, orderBy);
		if (cursor == null) return null;
		try {
			if (cursor.moveToNext()) {
				return getReplyInfoFromCursor(cursor);
			}
		} finally {
			cursor.close();
		}
		return null;
	}

	
	/**
	 * 
	 * @n<b>函数名称</b>     :clearReplyMe
	 * @brief	清除我的微博评论数据
	 * @see
	 * @since    Ver 1.1
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午4:07:38
	 */
	private void clearReplyMe() {
		mDb.delete(NdWeiboDataHelper.REPLY_ME_TABLE, null, null);
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setTagTopicInfoList
	 * @brief	保存话题微博列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param keyword
	 * @param  @param list
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:16:20
	 */
	public void setTagTopicInfoList(String keyword, TopicInfoList list) {
		
		setTopicInfoList(keyword, 0, list, NdWeiboDataHelper.TAG_TOPIC_TABLE);
		
	}
	
	private void clearTagTopicInfos() {
		
		mDb.delete(NdWeiboDataHelper.TAG_TOPIC_TABLE, null, null);
		
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :setTagTopicInfo
	 * @brief	设置话题微博
	 * @see
	 * @since    Ver 1.1
	 * @param  @param keyword
	 * @param  @param info
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:12:26
	 */
	public void setTagTopicInfo(String keyword, TopicInfo info) {
		
		setTopicInfo(keyword, 0, info, NdWeiboDataHelper.TAG_TOPIC_TABLE);
		
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getTagTopicInfoList
	 * @brief	查找话题对应的微博列表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param keyword
	 * @param  @return
	 * @return TopicInfoList
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:14:32
	 */
	public TopicInfoList getTagTopicInfoList(String keyword) {
		
		Cursor cursor = mDb.query(NdWeiboDataHelper.TAG_TOPIC_TABLE, null,
				TopicInfoColumns.KEY_WORD + "='" + keyword + "'", null, null,
				null, TopicInfoColumns.ID + " desc LIMIT 20");
		if (cursor == null) return null;
		TopicInfoList list = new TopicInfoList();
		try {
			while (cursor.moveToNext()) {
				list.add(getTopicInfoFromCursor(cursor));
			}
		}finally {
			cursor.close();
		}
		return list;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getTopicInfoFromCursor
	 * @brief	解析微博信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param cursor
	 * @param  @return
	 * @return TopicInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午5:16:24
	 */
	public static TopicInfo getTopicInfoFromCursor(Cursor cursor) {
		
		TopicInfo info = new TopicInfo();
		info.tid = cursor.getLong(cursor.getColumnIndex(TopicInfoColumns.ID));
		info.unit_id = cursor.getLong(cursor.getColumnIndex(TopicInfoColumns.UNIT_ID));
		info.group_id = cursor.getLong(cursor.getColumnIndex(TopicInfoColumns.GROUP_ID));
		info.weibo_tid = cursor.getLong(cursor.getColumnIndex(TopicInfoColumns.SINA_ID));
		info.root_tid = cursor.getLong(cursor.getColumnIndex(TopicInfoColumns.ROOT_TID));
		info.replys = cursor.getInt(cursor.getColumnIndex(TopicInfoColumns.REPLYS));
		info.glances = cursor.getInt(cursor.getColumnIndex(TopicInfoColumns.GLANCES));
		info.setPraised(cursor.getInt(cursor.getColumnIndex(TopicInfoColumns.PRAISED)));
		info.praises = cursor.getInt(cursor.getColumnIndex(TopicInfoColumns.PRAISES));
		info.forwards = cursor.getInt(cursor.getColumnIndex(TopicInfoColumns.FORWARDS));
		info.content = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.CONTENT));
		info.category = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.CATEGORY));
		info.from_string = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.FROM_STRING));
		info.article = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.ARTICLE));
		info.type = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.TYPE));
		info.post_time = cursor.getLong(cursor.getColumnIndex(TopicInfoColumns.POST_TIME));
		String user = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.USER));
		String attach = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.ATTACH));
		String image = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.IMAGE));
		int index = cursor.getColumnIndex(TopicInfoColumns.REQUEST_ID);
		if(index != -1){
			info.request_id = cursor.getLong(index);
		}
		String root_topic = cursor.getString(cursor.getColumnIndex(TopicInfoColumns.ROOT_TOPIC));
		
		try {
			info.user = new UserInfoParser().parse(TextUtils.isEmpty(user) ? null : new JSONObject(user));
			if(!TextUtils.isEmpty(attach)){
				info.attach = new AttachListParser().parse(new JSONArray(attach));
			}
			if(!TextUtils.isEmpty(image)){
				info.image = new ImageListparser().parse(new JSONArray(image));
			}
			if(!TextUtils.isEmpty(root_topic)){
				info.root_topic = new RootTopicParser().parse(new JSONObject(root_topic));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return info;
		
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getReplyInfoFromCursor
	 * @brief	解析评论信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param cursor
	 * @param  @return
	 * @return ReplyInfo
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-17上午10:00:20
	 */
	public static ReplyInfo getReplyInfoFromCursor(Cursor cursor) {
		ReplyInfo replyInfo = new ReplyInfo();
		replyInfo.rid = cursor.getLong(cursor.getColumnIndex(ReplyColumns.ID));
		replyInfo.content = cursor.getString(cursor.getColumnIndex(ReplyColumns.CONTENT));
		replyInfo.post_time = cursor.getLong(cursor.getColumnIndex(ReplyColumns.POST_TIME));
		replyInfo.from_string = cursor.getString(cursor.getColumnIndex(ReplyColumns.FROM_STRING));
		replyInfo.uid = cursor.getLong(cursor.getColumnIndex(ReplyColumns.UID));
		replyInfo.to_rid = cursor.getLong(cursor.getColumnIndex(ReplyColumns.TO_RID));
		replyInfo.to_tid = cursor.getLong(cursor.getColumnIndex(ReplyColumns.TO_TID));
		int index = cursor.getColumnIndex(ReplyColumns.REQUEST_ID);
		if(index != -1){
			replyInfo.request_id = cursor.getLong(index);
		}
		try {
			String user = cursor.getString(cursor.getColumnIndex(ReplyColumns.USER));
			String rootTopic = cursor.getString(cursor.getColumnIndex(ReplyColumns.ROOT_TOPIC));
			String to_reply = cursor.getString(cursor.getColumnIndex(ReplyColumns.TO_REPLY));
			replyInfo.user = new UserInfoParser().parse(TextUtils.isEmpty(user) ? null : new JSONObject(user));
			if(!TextUtils.isEmpty(rootTopic)){
				replyInfo.toTopic = new RootTopicParser().parse(new JSONObject(rootTopic));
			}
			if(!TextUtils.isEmpty(to_reply)){
				replyInfo.to_reply = new ToReplyParser().parse(new JSONObject(to_reply));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return replyInfo;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getPraisorFromCursor
	 * @brief	解析赞微博的用户信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param cursor
	 * @param  @return
	 * @return Praisor
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-2-14下午4:34:21
	 */
	public static  Praisor getPraisorFromCursor(Cursor cursor) {
		Praisor praisor = new Praisor();
		praisor.tid = cursor.getLong(cursor.getColumnIndex(PraiseUserColumns.TWEET_ID));
		praisor.id = cursor.getLong(cursor.getColumnIndex(PraiseUserColumns.ID));
		praisor.praise_time = cursor.getLong(cursor.getColumnIndex(PraiseUserColumns.PRAISE_TIME));
		String user = cursor.getString(cursor.getColumnIndex(PraiseUserColumns.USER));
		try {
			praisor.user = new UserInfoParser().parse(TextUtils.isEmpty(user) ? null :new JSONObject(user));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return praisor;
	}
	
	private UserInfo getUserFromCursor(Cursor cursor) {
		UserInfo user = new UserInfo();
		user.uid = cursor.getLong(cursor.getColumnIndex(UserColumns.ID));
		user.nickname = cursor.getString(cursor.getColumnIndex(UserColumns.NAME));
		user.gender = cursor.getInt(cursor.getColumnIndex(UserColumns.GENDER));
		return user;
	}

	public int getAttentionUserListMaxID() {
		int maxID = 0;
		Cursor cursor = mDb.query(NdWeiboDataHelper.IDOL_LIST_TABLE,
				new String[] { "max(id)" }, null, null, null, null, null);
		if (cursor != null) {
			try {
				cursor.moveToFirst();
				maxID = cursor.getInt(0);
			}finally{
				cursor.close();
			}
			
		}
		return maxID;
	}

	public int getAttentionUserListMinID() {
		int minID = 0;
		Cursor cursor = mDb.query(NdWeiboDataHelper.IDOL_LIST_TABLE,
				new String[] { "min(id)" }, null, null, null, null, null);
		if (cursor != null) {
			try {
				cursor.moveToFirst();
				minID = cursor.getInt(0);
			}
			finally{
				cursor.close();
			}
		}
		return minID;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :updateTopicInfo
	 * @brief	更新微博信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param topic
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-27下午4:04:48
	 */
	public void updateTopicInfo(TopicInfo topic) {
		mDb.beginTransaction();
		try {
			updateTopicInfo(topic, NdWeiboDataHelper.PUBLIC_TOPIC_TABLE);
			updateTopicInfo(topic, NdWeiboDataHelper.USER_TOPIC_TABLE);
			updateTopicInfo(topic, NdWeiboDataHelper.FRIEND_TOPIC_TABLE);
			updateTopicInfo(topic, NdWeiboDataHelper.AT_TOPIC_TABLE);
			
			mDb.setTransactionSuccessful();
		} catch (SQLException e) {

		} finally {
			mDb.endTransaction();
		}
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :updateTopicInfo
	 * @brief	更新微博内容
	 * @see
	 * @since    Ver 1.1
	 * @param  @param values
	 * @param  @param id
	 * @param  @param table
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-27上午10:08:26
	 */
	private void updateTopicInfo(TopicInfo topic,String table){
		TopicInfo ti = getTopicInfoById(topic.tid, table);
		if (ti != null) {
			ContentValues values = new ContentValues();
			setValuesByTopicInfo(values, topic);
			mDb.update(table, values,TopicInfoColumns.ID + " = " + topic.tid, null);
		}
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setLikeAttitudeInfo
	 * @brief	把相关的表都找一遍，看是否需要更新赞信息
	 * @see
	 * @since    Ver 1.1
	 * @param  @param id
	 * @param  @param isAttitude
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-17下午1:49:40
	 */
	public void setLikeAttitudeInfo(long id, boolean isAttitude) {
		mDb.beginTransaction();
		try {
			ContentValues values = new ContentValues();
			values.put(TopicInfoColumns.PRAISED, isAttitude ? 1 : 0);
			
			updateLikeInfo(values, id, isAttitude, NdWeiboDataHelper.PUBLIC_TOPIC_TABLE);
			updateLikeInfo(values, id, isAttitude, NdWeiboDataHelper.USER_TOPIC_TABLE);
			updateLikeInfo(values, id, isAttitude, NdWeiboDataHelper.FRIEND_TOPIC_TABLE);
			updateLikeInfo(values, id, isAttitude, NdWeiboDataHelper.AT_TOPIC_TABLE);
			
			mDb.setTransactionSuccessful();
		} catch (SQLException e) {

		} finally {
			mDb.endTransaction();
		}
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :updateLikeInfo
	 * @brief	更新赞信息到指定表
	 * @see
	 * @since    Ver 1.1
	 * @param  @param values
	 * @param  @param id
	 * @param  @param isAttitude
	 * @param  @param table
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-17下午1:45:56
	 */
	private void updateLikeInfo(ContentValues values,long id,boolean isAttitude,String table){
		TopicInfo ti = getTopicInfoById(id, table);
		if (ti != null) {
			int attitudeCount = ti.praises;
			attitudeCount = isAttitude ? ++attitudeCount : --attitudeCount;
			values.put(TopicInfoColumns.PRAISES, attitudeCount < 0 ? 0
					: attitudeCount);
			mDb.update(table, values,TopicInfoColumns.ID + " = " + id, null);
		}
	}
	

	public void setFlowerMessageInfo(FlowerMessageInfo info) {
		if (info == null) {
			return;
		}
		ContentValues values = new ContentValues();
		values.put(FlowerMsgColumns.UID, info.uid);
		values.put(FlowerMsgColumns.MSG_TEXT, info.msg_text);
		values.put(FlowerMsgColumns.MSG_VOICE, info.msg_voice);
		values.put(FlowerMsgColumns.MSG_VOICE_LOCAL, info.getMsgVoiceLocal());
		values.put(FlowerMsgColumns.MSG_TYPE, info.type);
		values.put(FlowerMsgColumns.MSG_STATE, info.state);
		values.put(FlowerMsgColumns.MSG_CONTENT_TYPE, info.contentType);
		mDb.replace(NdWeiboDataHelper.FLOWER_MSG_TABLE, null, values);
	}

	public void delFlowerMessageInfo(long uid, int type) {
		String whereClause = FlowerMsgColumns.UID + "=" + uid + " and "
				+ FlowerMsgColumns.MSG_TYPE + "=" + type;
		mDb.delete(NdWeiboDataHelper.FLOWER_MSG_TABLE, whereClause, null);
	}

	public FlowerMessageInfo getFlowerMessageInfo(long uid, int type) {
		String selection = FlowerMsgColumns.UID + "=" + uid + " and "
				+ FlowerMsgColumns.MSG_TYPE + "=" + type;
		Cursor cursor = mDb.query(NdWeiboDataHelper.FLOWER_MSG_TABLE, null,
				selection, null, null, null, null);
		if (cursor.moveToNext()) {
			try {
				FlowerMessageInfo info = getFlowerMsgFromCursor(cursor);
				return info;
			} finally{
				cursor.close();
			}
		}
		cursor.close();
		return null;
	}

	private FlowerMessageInfo getFlowerMsgFromCursor(Cursor cursor) {
		FlowerMessageInfo info = new FlowerMessageInfo();
		info.setUid(cursor.getLong(cursor.getColumnIndex(FlowerMsgColumns.UID)));
		info.setMsgText(cursor.getString(cursor
				.getColumnIndex(FlowerMsgColumns.MSG_TEXT)));
		info.setMsgVoice(cursor.getString(cursor
				.getColumnIndex(FlowerMsgColumns.MSG_VOICE)));
		info.setMsgVoiceLocal(cursor.getString(cursor
				.getColumnIndex(FlowerMsgColumns.MSG_VOICE_LOCAL)));
		info.setContentType(cursor.getInt(cursor
				.getColumnIndex(FlowerMsgColumns.MSG_CONTENT_TYPE)));
		info.setType(cursor.getInt(cursor
				.getColumnIndex(FlowerMsgColumns.MSG_TYPE)));
		info.setState(cursor.getInt(cursor
				.getColumnIndex(FlowerMsgColumns.MSG_STATE)));
		return info;
	}

	/**
	 * 
	 * @n<b>函数名称</b> :clearPraise
	 * @brief 删除赞消息表全部数据
	 * @see
	 * @since Ver 1.1
	 * @param
	 * @return void
	 * @<b>作者</b> : PanYun
	 * @<b>创建时间</b> : 2013-11-14下午5:29:20
	 */
	private void clearPraises() {
		mDb.delete(NdWeiboDataHelper.MY_PRAISED_TOPIC_TABLE, null, null);
	}
	
	/***************************** 时间戳表操作 *******************************/
	/**
	 * 
	 * @n<b>函数名称</b>     :setTimeStampTable
	 * @brief 更新或插入时间戳
	 * @see
	 * @since    Ver 1.1
	 * @param  @param name
	 * @param  @param ts
	 * @return void
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-13下午3:10:38
	 */
	public void setTimeStampTable(String name,long ts){
		if(TextUtils.isEmpty(name) || ts == 0){
			return;
		}	
		ContentValues values = new ContentValues();
		values.put(TimeStampColumns.TABLE_NAME, name);
		values.put(TimeStampColumns.TS, ts);
		// 用户可能已经在数据表里有了，所以用replace
		mDb.replace(NdWeiboDataHelper.TIMESTAMP_TABLE, null, values);		
	}
	/**
	 * 
	 * @n<b>函数名称</b>     :getTSFromTimeStampTable
	 * @brief 取得时间戳
	 * @see
	 * @since    Ver 1.1
	 * @param  @param name 表名
	 * @param  @return
	 * @return long
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-13下午2:42:56
	 */
	public long getTSFromTimeStampTable(String name){
		long ts = 0;
		if(TextUtils.isEmpty(name)){
			return ts;
		}
		Cursor cursor = mDb.query(NdWeiboDataHelper.TIMESTAMP_TABLE, null,
				TimeStampColumns.TABLE_NAME + "='" + name + "'"
				, null, null, null,null);
		if (cursor != null && cursor.moveToNext()) {
			try {
				ts = cursor.getLong(cursor.getColumnIndex(TimeStampColumns.TS));
			} finally{
				cursor.close();
			}
		}
		return ts;
	}	
}
